var express = require('express');
var router = express.Router();
var DateModule = require('../../databases/date')
var shops = DateModule.shops

// 渲染商品列表
router.get('/waterlist', async (req, res) => {
  const { keyword, category, page = 1, pageSize } = req.query
  console.log(pageSize);
  
  let query = {}
  if (keyword) {
    query.$or = [
      { name: { $regex: keyword, $options: 'i' } },
      { description: { $regex: keyword, $options: 'i' } }
    ]
  }
  if (category && category !== 'all') {
    query.category = category
  }
  const skip = (Number(page) - 1) * Number(pageSize)
  const total = await shops.countDocuments(query)
  const data = await shops.find(query).skip(skip).limit(Number(pageSize))
  res.send({ data, total })
})


// 商品详情
router.get('/detail', async (req, res) => {
  const id = req.query.id;
  if (!id) {
    return res.send({ error: '缺少商品id' });
  }
  const data = await shops.findOne({ _id: id });
  if (!data) {
    return res.send({ error: '商品不存在' });
  }
  res.send({
    data: data
  })
})
module.exports = router;